package com.msxt.config;

import com.msxt.interceptor.JwtTokenAdminInterceptor;
import com.msxt.interceptor.JwtTokenShangjiaInterceptor;
import com.msxt.interceptor.JwtTokenYonghuInterceptor;
import com.msxt.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    @Autowired
    private JwtTokenShangjiaInterceptor jwtTokenShangjiaInterceptor;

    @Autowired
    private JwtTokenYonghuInterceptor jwtTokenYonghuInterceptor;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");

        // 管理员接口拦截
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**", "/users/**")
                .excludePathPatterns("/admin/login", "/users/login");

        // 商家接口拦截 - 只处理商家专属路径
        registry.addInterceptor(jwtTokenShangjiaInterceptor)
                .addPathPatterns(
                        "/shangjia/**",
                        "/zaixiankefu/**" // 商家专属路径
                )
                .excludePathPatterns(
                        "/shangjia/login",
                        "/shangjia/register"
                );

        // 用户接口拦截 - 处理用户路径和共享路径
        registry.addInterceptor(jwtTokenYonghuInterceptor)
                .addPathPatterns(
                        "/yonghu/**",
                        "/minsukefang/**", // 共享路径
                        "/yudingxinxi/**", // 共享路径
                        "/news/**",
                        "/storeup/**"
                )
                .excludePathPatterns(
                        "/yonghu/login",
                        "/yonghu/register",
                        "/yonghu/resetPass",
                        // 排除上传文件路径
                        "/file/upload",
                        // 排除图片访问路径
                        "/upload/**"
                );
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

        // 添加静态资源处理
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/admin/")
                .addResourceLocations("classpath:/front/")
                .addResourceLocations("classpath:/public/")
                // 添加本地文件路径映射（根据实际情况调整）
                .addResourceLocations("file:" + System.getProperty("user.dir") + "/upload/");
    }

    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(new JacksonObjectMapper());
        converters.add(0, converter);
    }
}